package br.ucb.sigplan.action.impl;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.ucb.sigplan.action.Action;
import br.ucb.sigplan.beans.Conteudo;
import br.ucb.sigplan.crud.ConteudoCrud;

public class AdicionaConteudoTurmaListaAction implements Action {

	@SuppressWarnings("unchecked")
	@Override
	public String execute(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		HttpSession session = req.getSession();
		
		if (req.getParameterValues("conteudos") == null) {
			req.setAttribute("msg", "erro.selecaoConteudos");
		}
		else {
			List<Conteudo> conteudos = (List<Conteudo>) session.getAttribute("conteudos");
			List<Conteudo> conteudosTurma = (List<Conteudo>) session.getAttribute("conteudosTurma");
			List<String> conteudosSelecionados = Arrays.asList(req.getParameterValues("conteudos"));
			Conteudo tmp;
			
			for (String cont : conteudosSelecionados) {
				tmp = ConteudoCrud.findById(new Integer(cont));
				conteudos.remove(tmp);
				conteudosTurma.add(tmp);
			}
		}
		
		if (req.getParameter("acao").equalsIgnoreCase("cadastrar")) {
			return "/jsp/professor/conteudo/profCadastraConteudoTurma.jsp";
		}else {
			return "/jsp/professor/conteudo/profAlteraConteudoTurma.jsp";
		}
	}

}
